package com.woniuxy.bookmallmanagersrever.controller;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageInfo;
import com.wf.captcha.base.Captcha;
import com.woniuxy.bookmallmanagersrever.commons.LoginCodeEnum;
import com.woniuxy.bookmallmanagersrever.commons.LoginProperties;
import com.woniuxy.bookmallmanagersrever.entity.TManager;
import com.woniuxy.bookmallmanagersrever.entity.TRole;
import com.woniuxy.bookmallmanagersrever.service.ManagerService;
import com.woniuxy.bookmallmanagersrever.utils.JwtUtils;
import com.woniuxy.bookmallmanagersrever.vo.AccountVo;
import com.woniuxy.bookmallmanagersrever.vo.ManagerVo;
import com.woniuxy.bookmallmanagersrever.vo.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class ManagerController {
    @Autowired
    ManagerService managerService ;
    @Autowired
    JwtUtils jwtUtils ;

    @Autowired
    RedisTemplate<Object,Object> redisTemplate ;


    @PostMapping("/manager/login")
    public ResponseResult<TManager> login(@RequestBody AccountVo accountVo) {
        ResponseResult<TManager> result = new ResponseResult<>();
        try {
            //获取验证码
            /*Object o = redisTemplate.opsForValue().get(accountVo.getCodeUuid());
            if (o == null) {
                log.info("验证码过期---->"+accountVo.getCode());
            }else {
                if (accountVo.getCode().equals(o.toString())) {
                    log.info("验证码正确---->"+accountVo.getCode());
                }else {
                    log.info("验证码错误---->"+accountVo.getCode());
                }
            }*/
            TManager manager = managerService.login(accountVo.getUsername());
            if (manager == null) {
                result.setCode(201);
                result.setMsg("账户不存在!!!");
            }else {
                if (manager.getUserPassword().equals(accountVo.getPassword())){
                    result.setMsg("登录成功!!!");
                    result.setCode(200);
                    result.setData(manager);
                    //生成 token
                    accountVo.setId(manager.getId());
                    String token = jwtUtils.createToken(accountVo);
                    result.setToken(token);
                }else {
                    result.setCode(202);
                    result.setMsg("密码错误!!!");
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result ;
    }

    @GetMapping("/manager/code")
    public Object getCode(){
        LoginProperties loginProperties = new LoginProperties();
        Captcha captcha = loginProperties.getCaptcha();
        String uuid = "woniuxy"+ IdUtil.simpleUUID();
        //当验证码类型为 arithmetic时且长度 >= 2 时，captcha.text()的结果有几率为浮点型
        String captchaValue = captcha.text();
        if(captcha.getCharType()-1 == LoginCodeEnum.ARITHMETIC.ordinal() && captchaValue.contains(".")){
            captchaValue = captchaValue.split("\\.")[0];
        }
        // 保存
        //redisUtils.set(uuid,captchaValue,loginProperties.getLoginCode().getExpiration(), TimeUnit.MINUTES);
        redisTemplate.opsForValue().set(uuid,captchaValue,loginProperties.getLoginCode().getExpiration(), TimeUnit.MINUTES);
        // 验证码信息
        Map<String,Object> imgResult = new HashMap<String,Object>(2){{
            put("img",captcha.toBase64());
            put("uuid",uuid);
        }};
        return imgResult;
    }

    @GetMapping("/manager/list")
    public ResponseResult<PageInfo<ManagerVo>> list(
            @RequestParam(value = "now",defaultValue = "1") Integer now,
            @RequestParam(value = "size",defaultValue = "3") Integer size,
            String name){
        ResponseResult<PageInfo<ManagerVo>> result = new ResponseResult<>();
        try {
            PageInfo<ManagerVo> lists = managerService.findManagerByPage(now,size,name);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(lists);
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result ;
    }

    @PostMapping("/manager/add")
    public ResponseResult<Void> add(@RequestBody ManagerVo managerVo){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            managerService.add(managerVo);
            result.setCode(200);
            result.setMsg("添加成功!");
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("添加失败!");
        }
        return result ;
    }
}
